local DisplayObjectUtils = {}

function DisplayObjectUtils.collide(objA,objB)
   local left = objA.contentBounds.xMin <= objB.contentBounds.xMin and objA.contentBounds.xMax >= objB.contentBounds.xMin
   local right = objA.contentBounds.xMin >= objB.contentBounds.xMin and objA.contentBounds.xMin <= objB.contentBounds.xMax
   local up = objA.contentBounds.yMin <= objB.contentBounds.yMin and objA.contentBounds.yMax >= objB.contentBounds.yMin
   local down = objA.contentBounds.yMin >= objB.contentBounds.yMin and objA.contentBounds.yMin <= objB.contentBounds.yMax
   return (left or right) and (up or down)
end

function DisplayObjectUtils.pointInsideRect(point,obj)
   return point.x >= obj.contentBounds.xMin and point.x <= obj.contentBounds.xMax and point.y >= obj.contentBounds.yMin and point.y <= obj.contentBounds.yMax
end

function DisplayObjectUtils.showNotification(group,message,centerX,centerY,alpha,callback,dismissAction,textSize)
   if dismissAction == nil then dismissAction = true end
   local width,height = 300,100
   centerX,centerY = centerX or X*0.5,centerY or Y*0.5
   alpha = alpha or 200
   textSize = textSize or 20

   local notificationGroup = display.newGroup()
   if group then group:insert(notificationGroup) end

   local rect = display.newRoundedRect(notificationGroup,centerX - width*0.5,centerY - height*0.5,width,height,10)
   rect:setFillColor(0,100,0,alpha/2)

   width,height = width - 15,height - 15
   local rect2 = display.newRoundedRect(notificationGroup,centerX - width*0.5,centerY - height*0.5,width,height,10)
   rect2:setFillColor(0,100,0,alpha)

   local text = display.newText(notificationGroup,message,0,0,FONT_REGULAR,textSize)
   text.x,text.y=centerX,centerY

   function rect:tap()
      transition.to(self, { time=500, alpha=0, onComplete=function() DisplayObjectUtils:safeRemove(rect);rect = nil end} )
      transition.to(rect2, { time=500, alpha=0, onComplete=function() DisplayObjectUtils:safeRemove(rect2);rect2 = nil end} )
      transition.to(text, { time=525, alpha=0, onComplete=function() DisplayObjectUtils:safeRemove(text);text = nil;DisplayObjectUtils:safeRemove(notificationGroup) end} )

      if callback then callback() end

      return true
   end
   if dismissAction then
      rect:addEventListener('tap',rect)
   end
   return notificationGroup
end

function DisplayObjectUtils.addTapEventListener(object,callback,normalSequence,pressedSequence)
   object:removeEventListener('tap',object)
   object:removeEventListener('touch',object)

   function object:tap(event) return callback(event) end
   function object:touch(event)
      if object.sequence == 'disabled' then return false end
      if object.disabled then return false end

      if event.phase == 'ended' then
         if normalSequence then object:setSequence(normalSequence) end
         display.getCurrentStage():setFocus(nil)
         return callback(event)
      end
      if event.phase == 'began' then
         if pressedSequence then object:setSequence(pressedSequence) end

         display.getCurrentStage():setFocus(object)
      end
      return true
   end
   --object:addEventListener('tap',object)
   object:addEventListener('touch',object)
end

function DisplayObjectUtils.createBaseConfirmationPopup(msg,scale)
	local scale = 0.45
	local disabledGroup = display.newGroup()

	local disabledBg = display.newRect(disabledGroup,0,0,X,Y)
	disabledBg:setFillColor(0,0,0,200)

	local popupBg = display.newImage(disabledGroup,'resources/sprites/atlases/market/popup.png')
	popupBg:scale(scale,scale)
	popupBg:setReferencePoint(assert(display.CenterReferencePoint))
	popupBg.x,popupBg.y = X*0.5,Y*0.5

	local descText = display.newText(disabledGroup,msg,0,0,FONT_REGULAR,14)
	descText:setReferencePoint(display.CenterReferencePoint)
	descText.x,descText.y = X*0.55,Y*0.5

   local okButton = display.newSprite(
   	disabledGroup,
   	__marketAtlas.atlas,
   	{
      	{name='normal' ,frames={__marketAtlas.info:getFrameIndex('bok-1')}},
      	{name='pressed' ,frames={__marketAtlas.info:getFrameIndex('bok-2')}}
   	}
   )
   okButton:scale(scale*0.7,scale*0.7)
   okButton.x,okButton.y = X*0.45,Y*0.65

   local cancelButton = display.newSprite(
   	disabledGroup,
   	__marketAtlas.atlas,
   	{
      	{name='normal' ,frames={__marketAtlas.info:getFrameIndex('bcancel-1')}},
      	{name='pressed' ,frames={__marketAtlas.info:getFrameIndex('bcancel-2')}}
   	}
   )
   cancelButton:scale(scale,scale)
   cancelButton.x,cancelButton.y = X*0.6,Y*0.65

   return disabledGroup,popupBg,okButton,cancelButton
end

function DisplayObjectUtils.safeRemove(obj)
	if obj and obj.parent then
		display.remove(obj)
	end
end

return DisplayObjectUtils